package com.genius.action;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.genius.core.manager.StudentManager;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionSupport;

public class SearchStudentAction extends ActionSupport {
    
    
	private String firstName = "";
    
    private String lastName = "";
    
    private List studentList = null;
    
    private StudentManager studentManager = null;
    
    private String selectedStudentIds;

    private List batchList = null;
    
    private String selectedBatchId;
    
   /**
    * Method to search students
    * @return String
    */    
   public String searchStudent() 
    {
        studentList = studentManager.searchStudent(firstName, lastName);
        
        return this.SUCCESS;
    }


   /**
    * Method to search students to add them in a Batch
    * @return String
    */    
   public String SearchStudentForBatch()
    {
        
        return this.SUCCESS;
    }
   
   /**
    * Method to search Batch to add selected students
    * @return String
    */    
   public String chooseBatchForSelectedStudent()
    {
        System.out.println("Method chooseBatchForSelectedStudent Called");
        
		// Get All selected id from request
		String[] studentIds = ServletActionContext.getRequest().getParameterValues("studentIds");
        try{
           if( studentIds != null && studentIds.length > 0)
        	{
        		selectedStudentIds = makeCommaSeparatedString(studentIds);
        	}else { 
        		
        		addActionError("Please Select students from list");
        		return INPUT;
        	}
        
           this.batchList = this.studentManager.getBatchList(-1);
        	
        }catch (Exception e) {
			
        	System.out.println("****************Exception Occured in chooseBatchForSelectedStudent method === "+e.getMessage());
		}
        return this.SUCCESS;
    }
     
     
   /**
    * Method to search students to add them in a Batch
    * @return list
    * @throws Exception
    */    
   public String addStudentToBatch()
    {
        System.out.println("Method addStudentToBatch Called");
        
		// Get All selected id from request
		String batchId = ServletActionContext.getRequest().getParameter("batchId");
		System.out.println("Batch ID =======================>"+batchId);
		try{
        	if( batchId != null && batchId.length() > 0)
        	{
				List selectedStudentIdsList = parseString(selectedStudentIds, ",");
				studentManager.addStudentToBatch(batchId, selectedStudentIdsList);
        	
        	}else {
        		addActionError("Please Select a batch from list");
        		return INPUT;
        	}
        }catch (Exception e) {
        	System.out.println("****************Exception Occured in addStudentToBatch method()=== "+e.getMessage());
		}
        return this.SUCCESS;
    }   
   
  
   /*****   Class Level Method    ****/
   
	/**
	 * This method is for parsing a given string by any given delimiter 
	 * @param String string to be parsed
	 * @param delimiter for parsing
	 * @return List list of parsed element
	 */	
	private List parseString(String str,String delimiter)
	{
		List<Object> list=new ArrayList<Object>();
		StringTokenizer tokenizer=new StringTokenizer(str,delimiter);
		while(tokenizer.hasMoreTokens())
		{
			String token=tokenizer.nextToken();
			token = token.trim();
			list.add(token);
		}
		return list;
	}   
  
	/**
	 * Method to make comma separated string by given string attary
	 * @param string[] arr
	 * @return string
	 */
	private String makeCommaSeparatedString(String[] arr){
		
		String str="";
		for (int i = 0; i < arr.length; i++) {
			if(arr.length == 1){
				str = arr[i];
			}else {
				if(str == "")
				{
					str = arr[i];
				}else{
					str = str+","+arr[i];
				}
			}
		}		
		return str;
	}
	
   /******************   Setter and getter     *******************/
    
   /**
     * @return Returns the firstName.
     */
    public String getFirstName()
    {
        return firstName;
    }


    /**
     * @param firstName The firstName to set.
     */
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }


    /**
     * @return Returns the lastName.
     */
    public String getLastName()
    {
        return lastName;
    }


    /**
     * @param lastName The lastName to set.
     */
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }


    /**
     * @return Returns the studentList.
     */
    public List getStudentList()
    {
    	if (studentList == null ) {
    		studentList = studentManager.searchStudent(firstName, lastName);
    	}
        return studentList;
    }


    /**
     * @param studentList The studentList to set.
     */
    public void setStudentList(List studentList)
    {
        this.studentList = studentList;
    }


    /**
     * @return Returns the studentManager.
     */
    public StudentManager getStudentManager()
    {
        return studentManager;
    }


    /**
     * @param studentManager The studentManager to set.
     */
    public void setStudentManager(StudentManager studentManager)
    {
        this.studentManager = studentManager;
    }


	/**
	 * @return the selectedStudentIds
	 */
	public String getSelectedStudentIds() {
		return selectedStudentIds;
	}


	/**
	 * @param selectedStudentIds the selectedStudentIds to set
	 */
	public void setSelectedStudentIds(String selectedStudentIds) {
		this.selectedStudentIds = selectedStudentIds;
	}


	/**
	 * @return the batchList
	 */
	public List getBatchList() {
		return batchList;
	}


	/**
	 * @param batchList the batchList to set
	 */
	public void setBatchList(List batchList) {
		this.batchList = batchList;
	}


	/**
	 * @return the selectedBatchId
	 */
	public String getSelectedBatchId() {
		return selectedBatchId;
	}


	/**
	 * @param selectedBatchId the selectedBatchId to set
	 */
	public void setSelectedBatchId(String selectedBatchId) {
		this.selectedBatchId = selectedBatchId;
	}

    
}